home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / comm2 / kms20src.lha / KMSC / kmscookie.c < prev    next >
C/C++ Source or Header  |  1994-02-13  |  4KB  |  168 lines

  1. /**
  2.  **     KMSCookie.c
  3.  **
  4.  **     Prints a random fortune out of a database.
  5.  **
  6.  **     © Copyright 1993 Thomas Schwarz, Germany
  7.  **                      All Rights Reserved.
  8.  **/
  9.  
  10. // Includes
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <time.h>
  16. #include <clib/exec_protos.h>
  17. #include <clib/dos_protos.h>
  18. #include <dos/dos.h>
  19. #include <exec/types.h>
  20.  
  21. // Defines
  22.  
  23. #define USAGE "KMSCookie 1.0 -*- KMS Fortune Cookie Server ;-) (c)1993 Thomas Schwarz\n" \
  24.               "Randomly displays a fortune cookie from a chosen database.\n" \
  25.               "(Based on \"PFortune\" by Peter Simons)\n\n" \
  26.               "USAGE: %s <database>\n\n" \
  27.               "Default for <database> is \"KMS_Cookie.Dat\" in the current directory.\n\n" \
  28.               "Format of database:\n" \
  29.               "   Cookie 1 Line 1\n" \
  30.               "   Cookie 1 Line 2\n" \
  31.               "   ...\n" \
  32.               "   <empty line>\n" \
  33.               "   <empty line>\n" \
  34.               "   Cookie 2\n" \
  35.               "   ...\n"
  36.  
  37. #define BUFFERSIZE 256
  38. #define MARGIN 80
  39.  
  40. STRPTR  Version = "\0$VER: KMSCookie 1.0 ("__COMMODORE_DATE__")";
  41.  
  42. // Prototypes
  43.  
  44. int MyBreak(VOID);
  45. void OutCentered(int, char *);
  46. void MyExit(int, BPTR);
  47. int InLine(char *, BPTR);
  48.  
  49. int MyBreak(VOID)
  50.    {
  51.    return 0;
  52.    }
  53.  
  54. /**************************************************************************/
  55.  
  56. int main(int argc, char *argv[])
  57.    {
  58.    char *path = "KMS_Cookie.Dat", officialrule[BUFFERSIZE];
  59.    int next, character, firstempty;
  60.    unsigned long length, randpos;
  61.    BPTR fp;
  62.  
  63.    onbreak(MyBreak);
  64.  
  65.    if((argc == 2 && !strcmp("?", argv[1])) || argc > 2)
  66.       {
  67.       printf(USAGE, argv[0]);
  68.       MyExit(0, 0);
  69.       }
  70.  
  71.    if(argc == 2)
  72.       path = argv[1];
  73.    if(!(fp = Open(path, MODE_OLDFILE)))
  74.       {
  75.       printf("Can't open %s\n\n", path);
  76.       printf(USAGE, argv[0]);
  77.       MyExit(5, fp);
  78.       }
  79.  
  80.    Seek(fp, 0L, OFFSET_END);
  81.    length = Seek(fp, 0L, OFFSET_CURRENT);
  82.  
  83.    while(TRUE)
  84.       {
  85.       randpos = (unsigned long)clock() % length - 1;
  86.  
  87.       Seek(fp, randpos, OFFSET_BEGINNING);
  88.  
  89.       while((character = FGetC(fp)) != EOF)
  90.          {
  91.          if(character == '\n')
  92.             if((character = FGetC(fp)) != EOF && character == '\n')
  93.                if((character = FGetC(fp)) != EOF && character == '\n')
  94.                   break;
  95.          }
  96.  
  97.       if(!(InLine(officialrule, fp)))
  98.          continue;
  99.       else 
  100.          {
  101.          if(*officialrule == ';')
  102.             continue;
  103.          printf("\n");
  104.          OutCentered(MARGIN, officialrule);
  105.          }
  106.  
  107.       next = 0;
  108.       firstempty = FALSE;
  109.       while(TRUE)
  110.          {
  111.          if(!(InLine(officialrule, fp)))
  112.             MyExit(0, fp);
  113.          else
  114.             {
  115.             if(*officialrule == '\n' && firstempty)
  116.                MyExit(0, fp);
  117.             else
  118.                firstempty = TRUE;
  119.             OutCentered(MARGIN, officialrule);
  120.             if(next++ >= 10)
  121.                MyExit(0, fp);
  122.             }
  123.          continue;
  124.          }
  125.       }
  126.    }
  127.  
  128. void MyExit(int num, BPTR fp)
  129.    {
  130.    if(fp)
  131.       Close(fp);
  132.  
  133.    printf("\n");
  134.  
  135.    exit(num);
  136.    }
  137.  
  138. void OutCentered(int number, char *rule)
  139.    {
  140.    int offset;
  141.    char fortune[BUFFERSIZE];
  142.  
  143.    strcpy(fortune, rule);
  144.    offset = strlen(fortune);
  145.  
  146.    for(offset = ((number - offset) / 2); offset > 0; offset--)
  147.       strins(fortune, " ");
  148.  
  149.    printf("%s", fortune);
  150.    }
  151.  
  152. int InLine(char *string, BPTR fp)
  153.    {
  154.    int c;
  155.  
  156.    do
  157.       {
  158.       if((c = FGetC(fp)) == EOF)
  159.          return 0;
  160.       *string++ = c;
  161.       } while(c != '\n');
  162.  
  163.    *string = '\0';
  164.  
  165.    return 1;
  166.    }
  167.  
  168.